﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Timers;

namespace GameHungTrung.Entities
{
    class GiaCam : MySprite2D
    {
        #region MY CODE
        public enum Type { Ga, Vit, Ngan };
        public enum Direction {Left, Right};
        public static int constTOP = 80;

        private Random random;

        private Timer _TimerTaotrung;
        private Timer _TimerChuyenHuong;

        private Type _Type;
        private int _iSpeed;
        private List<Egg> _lstEgg;
        private int _iEgg;

        private Direction _HuongDiChuyen;

        public GiaCam(Texture2D texture, int left, int top, int width, int height)
        {
            random = new Random();
            _textureSprite = texture;
            _iLeft = left;
            _iTop = top;
            _iWidth = width;
            _iHeight = height;


            RandomDirection();
            _Type = Type.Ga;
            _iSpeed = 3;
            _iEgg = 0;
            InitListEgg();
            InitTimer();
        }

        //public GiaCam(Type type, ContentManager Content, int left)
        //{
        //    GetType(type, Content);
        //    _iLeft = left;
        //    _iTop = constTOP;
        //    _iWidth = _textureGiaCam.Width;
        //    _iHeight = _textureGiaCam.Height;
        //    _iSpeed = 5;
        //    _Sprite = new MySprite2D(_textureGiaCam, _iLeft, _iTop, _iWidth, _iHeight);
        //}
        public void InitTimer()
        {
            _TimerTaotrung = new Timer();
            _TimerChuyenHuong = new Timer();
            if (Game1.State == Game1.GameState.Running)
            {
                _TimerTaotrung.Interval = 5000;
                _TimerTaotrung.Elapsed += new ElapsedEventHandler(TimerTaotrung_Tick);
                _TimerTaotrung.Start();

                _TimerChuyenHuong.Interval = 3000;
                _TimerChuyenHuong.Elapsed += new ElapsedEventHandler(TimerChuyenHuong_Tick);
                _TimerChuyenHuong.Start();
            }
        }
        public void StartTimer()
        {
            _TimerTaotrung.Enabled = true;
            _TimerChuyenHuong.Enabled = true;
        }
        public void StopTimer()
        {
            _TimerTaotrung.Enabled = false;
            _TimerChuyenHuong.Enabled = false;
        }
        void TimerTaotrung_Tick(object sender, EventArgs e)
        {
            TaoTrungHoacChatThai();   
        }
        void TimerChuyenHuong_Tick(object sender, EventArgs e)
        {
            RandomDirection();
        }
        private void TaoTrungHoacChatThai()
        {
            Egg egg = new Egg(GameManager.textureEgg, -100, -100, GameManager.textureEgg.Width, GameManager.textureEgg.Height);
            int itype = random.Next(5);
            if (itype == 2)
            {
                egg.GetType(Egg.Type.ChatThai);
            }
            _lstEgg.Add(egg);
        }
        private void RandomDirection()
        {
            int direction = random.Next(2);
            switch (direction)
            {
                case 0:
                    _HuongDiChuyen = Direction.Left;
                    break;
                case 1:
                    _HuongDiChuyen = Direction.Right;
                    break;
                default:
                    break;
            }
        }
        public void MoveLeft()
        {
            if (_iLeft > 0)
            {
                _iLeft -= _iSpeed;
            }
            else
            {
                _HuongDiChuyen = Direction.Right;
            }
        }
        public void MoveRight()
        {
            if (_iLeft + _iWidth < Game1._iWidthWindow)
            {
                _iLeft += _iSpeed;
            }
            else
            {
                _HuongDiChuyen = Direction.Left;
            }
        }

        public void Move()
        {
            switch (_HuongDiChuyen)
            {
                case Direction.Left:
                    MoveLeft();
                    break;
                case Direction.Right:
                    MoveRight();
                    break;
                default:
                    break;
            }
        }

        public void HuyTrung(int i)
        {
            _iEgg = 0;
            _lstEgg.RemoveAt(i);
        }

        public void DeTrung()
        {
            ////if (idelay == 0)
            ////{
            if (_lstEgg.Count > 0)
            {
                if (_lstEgg[_iEgg].IsMoving == false)
                {
                    _lstEgg[_iEgg].IsMoving = true;
                    _lstEgg[_iEgg].LEFT = LEFT + WIDTH / 2 - _lstEgg[_iEgg].WIDTH / 2;
                    _lstEgg[_iEgg].TOP = TOP + HEIGHT;
                }
                _iEgg = (_iEgg + 1) % (_lstEgg.Count);
            }
               
            ////}
            ////idelay = (idelay + 1) % ndeday;
           
        }

       
        public void InitListEgg()
        {
            _lstEgg = new List<Egg>();
            Egg egg = new Egg(GameManager.textureEgg, -100, -100, GameManager.textureEgg.Width, GameManager.textureEgg.Height);
            _lstEgg.Add(egg);
        }
        public void GetType(Type type)
        {
            _Type = type;
            switch (_Type)
            {
                case Type.Ga:
                    _textureSprite = GameManager.textureGa;
                    _iWidth = _textureSprite.Width;
                    _iHeight = _textureSprite.Height;
                    break;
                case Type.Vit:
                    _textureSprite = GameManager.textureVit;
                    _iWidth = _textureSprite.Width;
                    _iHeight = _textureSprite.Height;
                    break;
                case Type.Ngan:
                    _textureSprite = GameManager.textureNgan;
                    _iWidth = _textureSprite.Width;
                    _iHeight = _textureSprite.Height;
                    break;
                default:
                    break;
            }
        }
        public List<Egg> LstEgg
        {
            get { return _lstEgg; }
            set { _lstEgg = value; }
        }
        public Type TYPE
        {
            get { return _Type; }
            set { _Type = value; }
        }
        public int SPEED
        {
            get { return _iSpeed; }
            set { _iSpeed = value; }
        }
        public int IEgg
        {
            get { return _iEgg; }
            set { _iEgg = value; }
        }
        public Direction HuongDiChuyen
        {
            get { return _HuongDiChuyen; }
            set { _HuongDiChuyen = value; }
        }
#endregion
    }
}
